#include "myclient.h"

myclient::myclient():client()
{
    this->money = 0;
    this->l = NULL;
}

myclient::~myclient()
{
    delete l;
}
void myclient::myclientstart()
{
    myclientintro();
    if(clientinit() == 1)
    {
        cout<<"Khoi tao thanh cong ."<<endl;
        clientgetparameters();
        if(clientcreatesocket() == 1)
        {
            cout<<"Tao socket thanh cong ."<<endl;
            if(clientconnect() == 1)
            {
                cout<<"Da ket noi server ."<<endl;
				Sleep(2500);
                myclientplaygame();
            }
            else
            {
                cout<<"Ket noi server loi ..."<<endl;
                clientclose();
                cin.get();
                cin.get();
                exit(-3);
            }
        }
        else
        {
            cout<<"Loi tao socket ..."<<endl;
            clientcleanup();
            cin.get();
            cin.get();
            exit(-2);
        }
    }
    else
    {
        cout<<"Loi khoi tao Winsock  ..."<<endl;
        clientcleanup();
        cin.get();
        cin.get();
        exit(-1);
    }
}
void myclient::myclientintro()
{
     cout<<"============================= LAP TRINH SOCKET======================="<<endl;
    cout<<"Ho ten             : Pham Van Diep "<<endl;
    cout<<"Mssv               : 0967023 "<<endl;
    cout<<"Ngon ngu           : c++ "<<endl;
    cout<<"Ten chuong trinh   : Tro choi dau tri "<<endl;
    cout<<"Port               : 9999 " <<endl;
    cout<<"------------------------------------------------------------  "<<endl;
    cout<<"Luat choi   :  "<<endl;
    cout<<"Server khoi tao 50 bao thu  "<<endl;
    cout<<"Voi cac gia tri : 10.000.000 <-> 2 ,5.000.000 <-> 2 "<<endl;
    cout<<"                  1.000.000  <-> 4 ,500.000   <-> 4 "<<endl;
    cout<<"                  100.000    <-> 4 ,50.000    <-> 4  "<<endl;
    cout<<"                  -100%      <-> 2 ,-50%      <-> 4 "<<endl;
    cout<<"                  -25%       <-> 4   "<<endl;
    cout<<"Nguoi choi o server nhap luong bao thu ma client duoc mo <1-5> "<<endl;
    cout<<"Khoi tao ngan phieu cua client :0 dong ,cho client connect  "<<endl;
    cout<<"Server gui so luong bao thu duoc mo cho client "<<endl;
    cout<<"Server cho nhan client chon bao thu muon mo "<<endl;
    cout<<"Server nhap cau hoi,cau tra loi gui cho client "<<endl;
    cout<<"Server cho nhan cau tra loi ,dung thi bao thu cua client "<<endl;
    cout<<"Khi da mo het so bao thu ma server cho phep client mo "<<endl;
    cout<<"Neu so bao thu client co < 0 ,tro choi ket thuc "<<endl;
    cout<<"Nguoc lai Server chon mua 1 bao thu,hoac 1 bao thu va ngan phieu,hoac 2 bao thu "<<endl;
    cout<<"Voi gia ma server muon mua,neu client dong y thi cap nhat bao thu cua client va ngan phieu "<<endl;
    cout<<"Neu khong client gui gia muon ban "<<endl;
    cout<<"Neu client client muon ket thuc chuong trinh ,client gui tin hieu ve server "<<endl;
    cout<<"Neu muon choi tiep ,tro choi quay lai tu dau "<<endl;
    cout<<" ------------------------------------------------------------ "<<endl;
}
void myclient::myclientplaygame()
{
	system("cls");
    int step,type,price,choose,st,num_l,trueorfalse, getid,lettermoney,currentstep;
    cout<<"\t\t\t Ngan phieu :"<<this->money<<" dong " <<endl;
    while(1)
    {

        string ty("Server muon mua ");
		cout<<"Dang cho server ... "<<endl;
        clientrecv();
        st = rec.datagettype();
        switch(st)
        {
        case -3:
            //chon bao thu da mo truoc do
            //cout<<string(50,'\n');     
            do
            {
				cout<<"Lua chon  khong hop le ."<<endl;
				cout<<"Ban chon bao thu so <1-50>: ";
                cin>>choose;
            }while(choose < 1 || choose >50 );
            sen.datasetvalue(2,choose,"","","","","",-1);
            clientsend();
            break;
        case 0:
            //thoat khoi chuong trinh
            cout<<"Game ket thuc ."<<endl;
            cout<<"Giai thuong cua ban hien co : "<<money<<" dong"<<endl;
            cin.get();
            cin.get();
            clientclose();
            exit(0);
			break;
        case 1:
            //gui co luong bao thu client duoc mo
            step = rec.datagetx();
            
                num_l = 0;
                l = new int[step];
                for(int i=0; i<step; i++)
                {
                    l[i] = 0;
                }
				cout<<"Ban co quyen mo "<<step<<" bao thu ."<<endl;
            
			break;
		case 2:
			system("cls");
			currentstep = rec.datagetansertrue();
			cout<<"Ban co quyen mo "<<step<<" bao thu ."<<endl;
			cout<<"\t\t\t\t\tLuot thu : "<<currentstep<<endl;;
			myclientprintlettermoney(num_l);
			//chon bao thu muon mo
            do
            {
                cout<<"Ban chon bao thu so <1-50>: ";
                cin>>choose;
				if(cin.fail())
				{
					cin.clear();
					cin.ignore(2000,'\n');
					cout<<"Ban can nhap so tu 1->50 : "<<endl;
					cin>>choose;
				}
            } while(choose < 1 || choose > 50 );
            sen.datasetvalue(2,choose,"","","","","",-1);
            clientsend();
            break;
        case 3:
            //nhan cau hoi va dap an gui cau tra loi
            //cout<<string(50,'\n');
            cout<<"Cau hoi: "<<endl;cout<<"--------"<<endl;cout<<rec.datagetwhat(0)<<endl<<endl;
            cout<<"Dap an : "<<endl;cout<<"--------"<<endl;
            cout<<"1 ."<<rec.datagetwhat(1)<<endl;cout<<"2 . "<<rec.datagetwhat(2)<<endl;
            cout<<"3 ."<<rec.datagetwhat(3)<<endl;cout<<"4 . "<<rec.datagetwhat(4)<<endl;
            cout<<"Tra loi <1-4>: ";
            cin>>choose;
			while(cin.fail())
			{
					cin.clear();
					cin.ignore(2000,'\n');
					cout<<"Ban can nhap so tu 1->4 : "<<endl;
					cin>>choose;
			
			}
            sen.datasetvalue(2,choose,"","","","","",-1);
            clientsend();
            break;
        case 4:
			myclientprintlettermoney(num_l);
            //chon co ban hay k? va co muon choi tiep k?
            type = rec.datagetx();
            //gia ca
            price = rec.datagetansertrue();
            switch(type)
            {
            case 1:
                ty += " bao thu";
                break;
            case 2:
                ty += " 2 bao thu ";
                break;
            case 3:
                ty += " bao thu va ngan phieu ";
                break;
            }
            int choo;
            choo = -1;
            cout<<ty<<" voi gia : "<<price*1000<<" dong."<<endl;
            while(choo != 0 && choo != 1)
            {
                cout<<"Ban co muon ban voi gia nay khong <1-co,0-khong> ? :";
                cin>>choo;
            }
            if(choo == 0)
            {
                int yourprice;
                cout<<"Gia ban muon ban : ";
                cin>>yourprice;
				yourprice = yourprice/1000;
                sen.datasetvalue(4,choo,"","","","","",yourprice);
            }
            else
            {
                sen.datasetvalue(4,choo,"","","","","",-1);
            }
            clientsend();
			break;
        case 5:
            //ban bao thu va nhan ngan phieu
            money = money + rec.datagetx()*1000;
            //trueorfal rec.datagetansertrue();
			 myclientprintlettermoney(num_l);
            break;
        case 6:
            /*
            * cap nhap bao thu ma client co
            */
           // cout<<"\t\t\t\t\tNgan phieu : "<<money<<" dong "<<endl;
            trueorfalse = rec.datagetx();
            if(trueorfalse == 1)
            {
                cout<<"Tra loi dung ."<<endl;
				trueorfalse = rec.datagetansertrue();
                l[num_l] = trueorfalse;
                num_l++;
               // myclientprintlettermoney(num_l);
            }
            else
            {
                cout<<"Tra loi sai ."<<endl;
               // myclientprintlettermoney(num_l);
            }
			Sleep(2500);
            break;
            /*
            *truong hop ban bao thu cho server
            */
        case 7:
			getid = rec.datagetx();
			if(getid != -1)
			{
				--num_l;--num_l;
			}
			else
			{
				--num_l;
			}
           // myclientprintlettermoney(num_l);
            break;
		case 8:
			cout<<"Gia ban dua ra qua cao,server khong chap nhan ."<<endl;
			cout<<"Server se mo bao thu cua ban ."<<endl;
			Sleep(1000);
			break;
		case 9:
			int exi;
            
           do{
			   cout<<"Ban co muon tiep tuc tro choi hay khong <1-co,0-khong> ? ";
					cin>>exi;
				while(cin.fail())
				{
						cin.clear();
						cin.ignore(2000,'\n');
						cout<<"Ban can nhap so tu 1 hoac 0 : "<<endl;
						cin>>choose;
			
				}
		   }while(exi != 0 && exi != 1);
           
            if(exi == 0)
            {
                sen.datasetvalue(0,-1,"","","","","",-1);
                clientsend();
            }
            else
            {
                sen.datasetvalue(-1,-1,"","","","","",-1);
                clientsend();
            }
            break;

		case 10:
			//mo bao thu
			cout<<"Server mo bao thu cua ban ."<<endl;
			cout<<"Gia tri cua bao thu : ";
			lettermoney = rec.datagetx();
			if(lettermoney > 0)
			{

				cout<<lettermoney*1000<<" dong."<<endl;
				money = money + lettermoney*1000;
			}
			else if(lettermoney == -100)
			{
				cout<<lettermoney<<" %."<<endl;
				money = -money;
			}
			else if(lettermoney == -50)
			{
				cout<<lettermoney<<" %."<<endl;
				money = money/2;
			}
			else if(lettermoney == -25)
			{
				cout<<lettermoney<<" %."<<endl;
				money = money/4;
			}
			else
			{
				cout<<" Bao thu khong co gi ."<<endl;
				//bao thu khong co gi ca
			}
			myclientprintlettermoney(num_l);
			Sleep(2000);
			break;
        }
    }
}
void myclient::myclientprintlettermoney(int num)
{
				cout<<"\t\t\t\t\tNgan luong : "<<money<<" dong ."<<endl;
				cout<<"\t\t\t\t\tBao thu ";
                for(int i = 0 ; i< num ; i++)
                {

                    cout<<" : "<<l[i];

                }
                cout<<endl;
				cout<<" ------------------------------------------------------------------------ "<<endl;
}
